﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Prism.Events;
using Infrastructure.Events;
using System.Reflection;
using Telerik.Windows.Controls;
using Infrastructure;

namespace ALPShell
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        static SplashScreen splashScreen;

        public static SplashScreen SplashScreen
        {
            get
            {
                if (splashScreen == null)
                {
                    splashScreen = new SplashScreen("Splash.jpg");
                }

                return splashScreen;
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;
            HideWarning();

            base.OnStartup(e);
            switch (Helper.GetConfigurationValue("Theme"))
            {
                case "Windows7":
                    StyleManager.ApplicationTheme = new Windows7Theme();
                    break;
                case "Metro":
                    StyleManager.ApplicationTheme = new Windows8Theme();
                    break;
                case "Office Black":
                    StyleManager.ApplicationTheme = new Office_BlackTheme();
                    break;
                case "Office Blue":
                    StyleManager.ApplicationTheme = new Office_BlueTheme();
                    break;
                case "Office Silver":
                    StyleManager.ApplicationTheme = new Office_SilverTheme();
                    break;
                case "Summner":
                    StyleManager.ApplicationTheme = new SummerTheme();
                    break;
                case "Vista":
                    StyleManager.ApplicationTheme = new VistaTheme();
                    break;
                case "Expression Dark":
                    StyleManager.ApplicationTheme = new Expression_DarkTheme();
                    break;
                case "Transparent":
                    StyleManager.ApplicationTheme = new TransparentTheme();
                    break;
                default:
                    break;
            }

            var bootstrapper = new ALPBootstrapper();

            SplashScreen.Show(false);
            bootstrapper.Run();

        }

        private void HideWarning()
        {
            var radCtrlAsm = Assembly.GetAssembly(typeof(RadControl));
            var type = radCtrlAsm.GetType("Telerik.Windows.Controls.TelerikLicense");
            var fld = type.GetField("licenseShown", BindingFlags.Static | BindingFlags.NonPublic);
            fld.SetValue(null, true);
        }

        void OnUnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;

            if (ex != null)
            {
                HandleException(ex);
            }
        }

        private static void HandleException(Exception e)
        {
            if (!Debugger.IsAttached)
            {
                ServiceLocator.Current.GetInstance<IEventAggregator>().GetEvent<AppExceptionEvent>().Publish(
                    new AppExceptionEventArg { Errors = new System.Collections.Generic.List<Exception>() { e } });
            }
        }
    }
}
